﻿using FreeSql;
using BJ.AuthEntity.Models;
using Syspetro.Deploy.FreeSqlDb;
using System.Collections.Generic;
using System;
using Syspetro.DepFreeSql;

namespace BJ.AuthEntity
{
    /// <summary>
    /// DBContext
    /// </summary>
    public class SyspetroAuthContext : BaseFreeSqlContext
    {
        /// <summary>
        /// 
        /// </summary>
        public DbSet<AdminUser> Users { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DbSet<AdminRole> Roles { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DbSet<AdminUserAuth> UserAuths { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="codefirst"></param>
        protected override void OnModelCreating(ICodeFirst codefirst)
        {

            codefirst.Entity<AdminUser>(eb =>
            {
                eb.HasOne(a => a.Role).HasForeignKey(t => t.RoleId).WithMany(t => t.Users);
            });
            codefirst.Entity<AdminRole>(eb =>
            {
                eb.HasMany(a => a.Users).WithOne(t => t.Role).HasForeignKey(t => t.RoleId);

                //var uid = Guid.NewGuid();
                //eb.HasData(new AdminRole
                //{
                //    IsAdmin = true,
                //    Level = 10,
                //    Name = "超级管理员",
                //    Users = new List<AdminUser>(new[]{
                //            new AdminUser{
                //                Id = uid,
                //                Name = "系统管理员", Enable2FA = "488249" , //Google验证:otpauth://totp/系统管理员?secret=OF3XK2LEMZUGU3TZPBVWYYJUHA4DENBZ&issuer=GMP"
                //                Auth = new AdminUserAuth{ AccountType = AccountType.Other, Account = "admin",  Password = "123456", Id = uid} }
                //        })
                //});
            });

            //codefirst.Entity<User>(eb =>
            //{
            //    var uid = Guid.NewGuid();
            //    eb.HasData(new User
            //    {
            //        Id = uid,
            //        Enable2FA = "488249",//Google验证:otpauth://totp/系统管理员?secret=OF3XK2LEMZUGU3TZPBVWYYJUHA4DENBZ&issuer=GMP"
            //        Name = "种子用户",
            //        Auth = new UserAuth { Id = uid, AccountType = AccountType.Other, Account = "bjkj", Password = "123456" }
            //    });
            //});
        }
    }
}
